package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_item.entity.WxRepairItem;
import com.sdp.wx.db.wx_repair_item.service.IWxRepairItemService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/WxRepairItemController")
public class WxRepairItemController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxRepairItemService iWxRepairItemService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairItemService, WxRepairItem.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxRepairItem repairItem) {
		if(repairItem==null) {
			return Result.error("维修项目信息为空");
		}
		if(StringUtils.isEmpty(repairItem.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairItemService, WxRepairItem.class, new String[] {"itemNo"}, "keyId", repairItem, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修项目编号不能重复");
			}
			iWxRepairItemService.save(repairItem);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairItemService, WxRepairItem.class, new String[] {"itemNo"}, "keyId", null, repairItem, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修项目编号不能重复");
			}
			iWxRepairItemService.updateById(repairItem);
		}
		
		return Result.ok();
	}
}

